<?php

namespace App\Http\Controllers\Stock;

use App\Http\Controllers\BaseController;
use App\Models\Stock\StockField;
use App\Models\Stock\StockFieldRule;
use Illuminate\Http\Request;

/**
 * 库存字段管理
 */
class StockFieldController extends BaseController
{
    /**
     * 获取字段权限
     */
    public function getruinfo(Request $request)
    {
        //查询角色是否配置
        $utype = $request->utype ?? 1; //1管理员，2产品经理，3税务会计，4财务，5仓库，6采购，7业务
        if (!in_array($utype, [1, 2, 3, 4, 5, 6, 7])) {
            // return response()->json(['code' => '001', 'msg' => '参数错误']);
            return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['field_rule' => []]]);
        }
        $rule_info = StockFieldRule::where(['cpid' => $this->mangerinfo['cpid'], 'utype' => $utype])->first();
        if (!$rule_info) {

            $field_rule = StockField::get()->toArray();
            foreach ($field_rule as &$val) {
                $val['ischecked'] = 0;
            }
        } else {
            $field_rule = json_decode($rule_info->field_rule, true);
        }
        // echo json_encode($field_rule);exit;
        //[{"id":1,"field_title":"\u4ed3\u5e93","field_name":"warehouse_name","ischecked":1},{"id":2,"field_title":"\u94a2\u6750\u7f16\u7801","field_name":"archive_no","ischecked":1},{"id":3,"field_title":"\u54c1\u540d","field_name":"archive_name","ischecked":1},{"id":4,"field_title":"\u89c4\u683c","field_name":"archive_spec","ischecked":1},{"id":5,"field_title":"\u6750\u8d28","field_name":"archive_material","ischecked":1},{"id":6,"field_title":"\u53ef\u7528\u91cd\u91cf","field_name":"can_use_weight","ischecked":1},{"id":7,"field_title":"\u53ef\u7528\u4ef6\u6570","field_name":"can_use_pieces","ischecked":1},{"id":8,"field_title":"\u5df2\u9501\uff08\u5f85\u51fa\uff09\u4ef6\u6570","field_name":"locked_pieces","ischecked":1},{"id":9,"field_title":"\u5df2\u9501\uff08\u5f85\u51fa\uff09\u91cd\u91cf","field_name":"locked_weight","ischecked":1},{"id":10,"field_title":"\u73b0\u5b58\u4ef6\u6570","field_name":"current_pieces","ischecked":1},{"id":11,"field_title":"\u73b0\u5b58\u91cd\u91cf","field_name":"current_weight","ischecked":1},{"id":12,"field_title":"\u5f85\u5165\u5e93\u6570","field_name":"storage_waiting_pieces","ischecked":1},{"id":13,"field_title":"\u5f85\u5165\u5e93\u91cd\u91cf","field_name":"storage_waiting_weight","ischecked":1},{"id":14,"field_title":"\u91c7\u8d2d\u4ef7-\u57fa\u4ef7","field_name":"supply_base_price","ischecked":1},{"id":15,"field_title":"\u91c7\u8d2d\u4ef7-\u52a0\u4ef7","field_name":"supply_add_price","ischecked":1},{"id":16,"field_title":"\u91c7\u8d2d\u4ef7-\u5355\u4ef7","field_name":"supply_price","ischecked":1},{"id":17,"field_title":"\u6307\u5bfc\u4ef7-\u57fa\u4ef7","field_name":"costom_base_price","ischecked":1},{"id":18,"field_title":"\u6307\u5bfc\u4ef7-\u52a0\u4ef7","field_name":"costom_add_price","ischecked":1},{"id":19,"field_title":"\u6307\u5bfc\u4ef7-\u5355\u4ef7","field_name":"costom_price","ischecked":1}]
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['field_rule' => $field_rule]]);
    }

    /**
     * 编辑字段权限
     */
    public function doposteditrule(Request $request)
    {
        $utype = $request->utype ?? 1; //1管理员，2产品经理，3税务会计，4财务，5仓库，6采购，7业务
        $field_rule = $request->field_rule ?? '';
        $field_rule = is_array($field_rule) ? $field_rule : (is_array(json_decode($field_rule, true)) ? json_decode($field_rule, true) : []);
        if (!in_array($utype, [1, 2, 3, 4, 5, 6, 7])) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        $rulecount = StockField::count();
        if ($rulecount != count($field_rule)) {
            return response()->json(['code' => '001', 'msg' => '参数错误!']);
        }
        $rule_info = StockFieldRule::where(['cpid' => $this->mangerinfo['cpid'], 'utype' => $utype])->first();

        if ($rule_info) {
            StockFieldRule::where(['cpid' => $this->mangerinfo['cpid'], 'utype' => $utype])->update(['update_at' => time(), 'field_rule' => json_encode($field_rule)]);
        } else {
            StockFieldRule::create(['cpid' => $this->mangerinfo['cpid'], 'utype' => $utype, 'create_at' => time(), 'update_at' => time(), 'field_rule' => json_encode($field_rule)]);
        }
        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => ['field_rule' => $field_rule]]);
    }
}
